/**
 * 缓存，用于提高 计算 菜单项 的效率
 */
import {RouteItem} from "../../Pages/type";
import {config2Menu} from './menuIter'

let  config2MenuCache:{[key:string]:any} = {
};

function getKey(rs: RouteItem[] | RouteItem,result:string) {
  if(!Array.isArray(rs)){
    return rs.key + result;
  }
  for(let r in rs){
    let item = rs[r];
    result += item.key;
    if(item.children){
      result+= getKey(item.children,result);
    }
  }
  return result;
}

export function getConfig2Menu(rs : RouteItem | RouteItem[], authInfo:string[]){
  const t = getKey(rs,"") + "#" +authInfo.join(",");
  if(config2MenuCache[t]){
     return config2MenuCache[t];
  }
  const info = config2Menu(rs, authInfo);
  if(info){
    config2MenuCache[t] = info;
    return info;
  }
  return null;
}
